/* 
 * File:   main.c
 * Author: Pirx
 *
 * Created on 1 de febrero de 2015, 7:48
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
 * 
 */
int main(int argc, char** argv) {

    char EcgFile[] = "SH_Ecg_20141021210651_filtro7_caballo2_invertida";
    char EcgDir[] = "Graficas/";
    
    char EcgIn[80];
    char EcgOut[80];

    strcpy(EcgIn, EcgDir);
    strcat(EcgIn, EcgFile); 
    strcat(EcgIn, ".txt");
    strcpy(EcgOut, EcgDir);
    strcat(EcgOut, EcgFile); 
    strcat(EcgOut, ".dat");
    
    printf("Entrada = %s\n", EcgIn);
    printf("Salida = %s\n", EcgOut);
    
    FILE *fpin, *fpout;
    fpin = fopen (EcgIn, "r");

    if (fpin == NULL){
        
        printf("\nError de Apertura, Archivo inexistente.\n\n");
        
    } else {
        
        fpout = fopen (EcgOut, "w");
        int     n = 0;
        int     i = 0;
        char    car;
        char    s;
        char    st[10];
        
        s = '\n';
        printf("%c", s);
        fputc(s, fpout);
        s = '\t';
        printf("%c", s);
        fputc(s, fpout);
        
        while (feof(fpin) == 0){
            car = fgetc(fpin);
            if (car == '\n'){
                n = -1;
                s = '\n';
                printf("%c", s);
                fputc(s, fpout);
                s = '\t';
                printf("%c", s);
                fputc(s, fpout);
            } else if (car >= 0x30){
                if (n > 1){
                    if ((n%2) == 0){
                        strcpy(st, "0x");
                        printf("%s", st);
                        printf("%c", car);
                        if ((i%2) == 0){
                            fputs(st, fpout);
                            fputc(car, fpout);
                        }
                    } else {
                        printf("%c", car);
                        if ((i%2) == 0){
                                fputc(car, fpout);
                        }
                        s = ',';
                        printf("%c", s);
                        if ((i%2) == 0){
                                fputc(s, fpout);
                        }
                        i++;
                    }
                }
            }
            n++;            
        }

        fclose(fpout);

        printf("\nOK.\n\n");
    }

    fclose(fpin);
    
    return (EXIT_SUCCESS);
}

